{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "479b1b1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65b8b34e",
   "metadata": {},
   "source": [
    "### 读取数据，并且对数据格式作出一些调整，方便后续分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d5a752c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th>StockCode</th>\n",
       "      <th>Description</th>\n",
       "      <th>Quantity</th>\n",
       "      <th>InvoiceDate</th>\n",
       "      <th>UnitPrice</th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Country</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>536365</td>\n",
       "      <td>85123A</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER</td>\n",
       "      <td>6</td>\n",
       "      <td>2010/12/1 8:26</td>\n",
       "      <td>2.55</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>536365</td>\n",
       "      <td>71053</td>\n",
       "      <td>WHITE METAL LANTERN</td>\n",
       "      <td>6</td>\n",
       "      <td>2010/12/1 8:26</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>536365</td>\n",
       "      <td>84406B</td>\n",
       "      <td>CREAM CUPID HEARTS COAT HANGER</td>\n",
       "      <td>8</td>\n",
       "      <td>2010/12/1 8:26</td>\n",
       "      <td>2.75</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029G</td>\n",
       "      <td>KNITTED UNION FLAG HOT WATER BOTTLE</td>\n",
       "      <td>6</td>\n",
       "      <td>2010/12/1 8:26</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029E</td>\n",
       "      <td>RED WOOLLY HOTTIE WHITE HEART.</td>\n",
       "      <td>6</td>\n",
       "      <td>2010/12/1 8:26</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  InvoiceNo StockCode                          Description  Quantity  \\\n",
       "0    536365    85123A   WHITE HANGING HEART T-LIGHT HOLDER         6   \n",
       "1    536365     71053                  WHITE METAL LANTERN         6   \n",
       "2    536365    84406B       CREAM CUPID HEARTS COAT HANGER         8   \n",
       "3    536365    84029G  KNITTED UNION FLAG HOT WATER BOTTLE         6   \n",
       "4    536365    84029E       RED WOOLLY HOTTIE WHITE HEART.         6   \n",
       "\n",
       "      InvoiceDate  UnitPrice  CustomerID         Country  \n",
       "0  2010/12/1 8:26       2.55     17850.0  United Kingdom  \n",
       "1  2010/12/1 8:26       3.39     17850.0  United Kingdom  \n",
       "2  2010/12/1 8:26       2.75     17850.0  United Kingdom  \n",
       "3  2010/12/1 8:26       3.39     17850.0  United Kingdom  \n",
       "4  2010/12/1 8:26       3.39     17850.0  United Kingdom  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取并且查看数据\n",
    "df = pd.read_csv(\"Online Retail(1).csv\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d7e55843",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将时间数据设置为pandas的数据格式\n",
    "df[\"InvoiceDate\"] = pd.to_datetime(df[\"InvoiceDate\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "692ee1bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 注：由于最近一次购物(Recency)是针对某个时间点计算的，而最后订货日期是 2011-12-09，因此\n",
    "# 我们把 2011-12-10 当作今天，来计算 Recency。\n",
    "df[\"target_time\"] = pd.to_datetime(\"2011-12-10\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0e1769ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算 Recency\n",
    "df[\"Recency\"] = pd.to_datetime(df[\"target_time\"]) - pd.to_datetime(df[\"InvoiceDate\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7c1c4e78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th>StockCode</th>\n",
       "      <th>Description</th>\n",
       "      <th>Quantity</th>\n",
       "      <th>InvoiceDate</th>\n",
       "      <th>UnitPrice</th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Country</th>\n",
       "      <th>target_time</th>\n",
       "      <th>Recency</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>536365</td>\n",
       "      <td>85123A</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.55</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>15.30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>536365</td>\n",
       "      <td>71053</td>\n",
       "      <td>WHITE METAL LANTERN</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>536365</td>\n",
       "      <td>84406B</td>\n",
       "      <td>CREAM CUPID HEARTS COAT HANGER</td>\n",
       "      <td>8</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.75</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>22.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029G</td>\n",
       "      <td>KNITTED UNION FLAG HOT WATER BOTTLE</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029E</td>\n",
       "      <td>RED WOOLLY HOTTIE WHITE HEART.</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  InvoiceNo StockCode                          Description  Quantity  \\\n",
       "0    536365    85123A   WHITE HANGING HEART T-LIGHT HOLDER         6   \n",
       "1    536365     71053                  WHITE METAL LANTERN         6   \n",
       "2    536365    84406B       CREAM CUPID HEARTS COAT HANGER         8   \n",
       "3    536365    84029G  KNITTED UNION FLAG HOT WATER BOTTLE         6   \n",
       "4    536365    84029E       RED WOOLLY HOTTIE WHITE HEART.         6   \n",
       "\n",
       "          InvoiceDate  UnitPrice  CustomerID         Country target_time  \\\n",
       "0 2010-12-01 08:26:00       2.55     17850.0  United Kingdom  2011-12-10   \n",
       "1 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "2 2010-12-01 08:26:00       2.75     17850.0  United Kingdom  2011-12-10   \n",
       "3 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "4 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "\n",
       "            Recency  total  \n",
       "0 373 days 15:34:00  15.30  \n",
       "1 373 days 15:34:00  20.34  \n",
       "2 373 days 15:34:00  22.00  \n",
       "3 373 days 15:34:00  20.34  \n",
       "4 373 days 15:34:00  20.34  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算每个订单的总金额\n",
    "df[\"total\"] = df[\"Quantity\"] * df[\"UnitPrice\"]\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db6f6e44",
   "metadata": {},
   "source": [
    "#### 设置分析所需的星期、月份等属性，方便按此聚合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b3d57b53",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"星期\"] = df[\"InvoiceDate\"].dt.dayofweek+1\n",
    "df[\"月份\"] = df[\"InvoiceDate\"].dt.month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2549c3c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th>StockCode</th>\n",
       "      <th>Description</th>\n",
       "      <th>Quantity</th>\n",
       "      <th>InvoiceDate</th>\n",
       "      <th>UnitPrice</th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Country</th>\n",
       "      <th>target_time</th>\n",
       "      <th>Recency</th>\n",
       "      <th>total</th>\n",
       "      <th>星期</th>\n",
       "      <th>月份</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>536365</td>\n",
       "      <td>85123A</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.55</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>15.30</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>536365</td>\n",
       "      <td>71053</td>\n",
       "      <td>WHITE METAL LANTERN</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>536365</td>\n",
       "      <td>84406B</td>\n",
       "      <td>CREAM CUPID HEARTS COAT HANGER</td>\n",
       "      <td>8</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.75</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>22.00</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029G</td>\n",
       "      <td>KNITTED UNION FLAG HOT WATER BOTTLE</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029E</td>\n",
       "      <td>RED WOOLLY HOTTIE WHITE HEART.</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  InvoiceNo StockCode                          Description  Quantity  \\\n",
       "0    536365    85123A   WHITE HANGING HEART T-LIGHT HOLDER         6   \n",
       "1    536365     71053                  WHITE METAL LANTERN         6   \n",
       "2    536365    84406B       CREAM CUPID HEARTS COAT HANGER         8   \n",
       "3    536365    84029G  KNITTED UNION FLAG HOT WATER BOTTLE         6   \n",
       "4    536365    84029E       RED WOOLLY HOTTIE WHITE HEART.         6   \n",
       "\n",
       "          InvoiceDate  UnitPrice  CustomerID         Country target_time  \\\n",
       "0 2010-12-01 08:26:00       2.55     17850.0  United Kingdom  2011-12-10   \n",
       "1 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "2 2010-12-01 08:26:00       2.75     17850.0  United Kingdom  2011-12-10   \n",
       "3 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "4 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "\n",
       "            Recency  total  星期  月份  \n",
       "0 373 days 15:34:00  15.30   3  12  \n",
       "1 373 days 15:34:00  20.34   3  12  \n",
       "2 373 days 15:34:00  22.00   3  12  \n",
       "3 373 days 15:34:00  20.34   3  12  \n",
       "4 373 days 15:34:00  20.34   3  12  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f67404ab",
   "metadata": {},
   "source": [
    "### 按星期聚合总销售额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "3f9c0b8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>星期</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1588609.431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1966182.791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1734147.010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2112519.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>1540610.811</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>7</td>\n",
       "      <td>805678.891</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   星期        total\n",
       "0   1  1588609.431\n",
       "1   2  1966182.791\n",
       "2   3  1734147.010\n",
       "3   4  2112519.000\n",
       "4   5  1540610.811\n",
       "5   7   805678.891"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_w = df.groupby(\"星期\").agg({\"total\":\"sum\"}).reset_index()\n",
    "df_by_w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8d0fd7c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cdf28e58",
   "metadata": {},
   "outputs": [],
   "source": [
    "bar = (\n",
    "    Bar()\n",
    "    .add_xaxis(df_by_w[\"星期\"].to_list())\n",
    "    .add_yaxis(\"销售额\", df_by_w[\"total\"].to_list())\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"销售额对比图\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19f53186",
   "metadata": {},
   "source": [
    "#### 以星期为单位，周几的销售额最高？ 答案是周四"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "97cbc731",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"db8d440c33724b12b46c63c4cb84dda5\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_db8d440c33724b12b46c63c4cb84dda5 = echarts.init(\n",
       "                    document.getElementById('db8d440c33724b12b46c63c4cb84dda5'), 'white', {renderer: 'canvas'});\n",
       "                var option_db8d440c33724b12b46c63c4cb84dda5 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"\\u9500\\u552e\\u989d\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                1588609.4309997542,\n",
       "                1966182.7909996351,\n",
       "                1734147.0099997583,\n",
       "                2112518.999999767,\n",
       "                1540610.8109998337,\n",
       "                805678.8910000041\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u9500\\u552e\\u989d\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u9500\\u552e\\u989d\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                1,\n",
       "                2,\n",
       "                3,\n",
       "                4,\n",
       "                5,\n",
       "                7\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u9500\\u552e\\u989d\\u5bf9\\u6bd4\\u56fe\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_db8d440c33724b12b46c63c4cb84dda5.setOption(option_db8d440c33724b12b46c63c4cb84dda5);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x25dcedcd700>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bar.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8dcbc9f",
   "metadata": {},
   "source": [
    "#### 利用 RFM 模型，对 United Kingdom 的用户进行分类"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de36b8de",
   "metadata": {},
   "source": [
    "#### 按客户ID聚合，对发票订单号进行计数，对每个发票的销售额进行汇总，对recency取最小值（作为该顾客的recency值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "be6caace",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_by_c = df.groupby(\"CustomerID\").agg({\"InvoiceNo\":\"count\", \"total\":\"sum\", \"Recency\":\"min\"}).reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6951e2ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Frequency</th>\n",
       "      <th>Monetary</th>\n",
       "      <th>Recency</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12346.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.00</td>\n",
       "      <td>325 days 13:43:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12347.0</td>\n",
       "      <td>182</td>\n",
       "      <td>4310.00</td>\n",
       "      <td>2 days 08:08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12348.0</td>\n",
       "      <td>31</td>\n",
       "      <td>1797.24</td>\n",
       "      <td>75 days 10:47:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12349.0</td>\n",
       "      <td>73</td>\n",
       "      <td>1757.55</td>\n",
       "      <td>18 days 14:09:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12350.0</td>\n",
       "      <td>17</td>\n",
       "      <td>334.40</td>\n",
       "      <td>310 days 07:59:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CustomerID  Frequency  Monetary           Recency\n",
       "0     12346.0          2      0.00 325 days 13:43:00\n",
       "1     12347.0        182   4310.00   2 days 08:08:00\n",
       "2     12348.0         31   1797.24  75 days 10:47:00\n",
       "3     12349.0         73   1757.55  18 days 14:09:00\n",
       "4     12350.0         17    334.40 310 days 07:59:00"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_c.columns = [\"CustomerID\",\"Frequency\",\"Monetary\",\"Recency\"]\n",
    "df_by_c.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "645f895d",
   "metadata": {},
   "source": [
    "### 求出各列的四分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e2d7eceb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.25     17.0\n",
       "0.50     42.0\n",
       "0.75    102.0\n",
       "Name: Frequency, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_c[\"Frequency\"].quantile([0.25,0.5,0.75])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e30cda43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.25     293.3625\n",
       "0.50     648.0750\n",
       "0.75    1611.7250\n",
       "Name: Monetary, dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_c[\"Monetary\"].quantile([0.25,0.5,0.75])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "458cf532",
   "metadata": {},
   "source": [
    "##### 将Recency这列的数据格式转为整数（取day这个属性就可以了）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "42e20b6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_by_c[\"Recency\"] = df_by_c[\"Recency\"].dt.days"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5fb46dc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.25     16.0\n",
       "0.50     50.0\n",
       "0.75    143.0\n",
       "Name: Recency, dtype: float64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_c[\"Recency\"].quantile([0.25,0.5,0.75])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3488e8e0",
   "metadata": {},
   "source": [
    "#### 按照刚才求得的各项指标的四分位数，制定相应的函数，计算RFM的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "977f1c27",
   "metadata": {},
   "outputs": [],
   "source": [
    "def decide_F(score):\n",
    "    if score>102:\n",
    "        return 1\n",
    "    elif score>42:\n",
    "        return 2\n",
    "    elif score>17:\n",
    "        return 3\n",
    "    else:\n",
    "        return 4\n",
    "    \n",
    "def decide_M(score):\n",
    "    if score>1611.725:\n",
    "        return 1\n",
    "    elif score>648.075:\n",
    "        return 2\n",
    "    elif score>293.3625:\n",
    "        return 3\n",
    "    else:\n",
    "        return 4\n",
    "    \n",
    "def decide_R(score):\n",
    "    if score<16:\n",
    "        return 1\n",
    "    elif score<50:\n",
    "        return 2\n",
    "    elif score<143:\n",
    "        return 3\n",
    "    else:\n",
    "        return 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "03008539",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_by_c[\"F\"] = df_by_c.apply(lambda x: decide_F(x.Frequency), axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d5e44919",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_by_c[\"M\"] = df_by_c.apply(lambda x: decide_F(x.Monetary), axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "db4e1c29",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_by_c[\"R\"] = df_by_c.apply(lambda x: decide_R(x.Recency), axis = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "708a0e7c",
   "metadata": {},
   "source": [
    "#### 看一下求得的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "fc0d259e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Frequency</th>\n",
       "      <th>Monetary</th>\n",
       "      <th>Recency</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "      <th>R</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12346.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.00</td>\n",
       "      <td>325</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12347.0</td>\n",
       "      <td>182</td>\n",
       "      <td>4310.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12348.0</td>\n",
       "      <td>31</td>\n",
       "      <td>1797.24</td>\n",
       "      <td>75</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12349.0</td>\n",
       "      <td>73</td>\n",
       "      <td>1757.55</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12350.0</td>\n",
       "      <td>17</td>\n",
       "      <td>334.40</td>\n",
       "      <td>310</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CustomerID  Frequency  Monetary  Recency  F  M  R\n",
       "0     12346.0          2      0.00      325  4  4  4\n",
       "1     12347.0        182   4310.00        2  1  1  1\n",
       "2     12348.0         31   1797.24       75  3  1  3\n",
       "3     12349.0         73   1757.55       18  2  1  2\n",
       "4     12350.0         17    334.40      310  4  1  4"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_c.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13436dfc",
   "metadata": {},
   "source": [
    "###  根据定义，制定对客户进行类型划分的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "041903a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def decide_type(f, m, r):\n",
    "    if f == 1 and m == 1 and r == 1:\n",
    "        return \"最佳客户\"\n",
    "    if f == 1 and m == 1 and r == 3:\n",
    "        return \"近流失客户\"\n",
    "    if f == 1 and m == 1 and r == 4:\n",
    "        return \"流失客户\"\n",
    "    if f == 4 and m == 4 and r == 4:\n",
    "        return \"流失廉价客户\"\n",
    "    if f == 1:\n",
    "        return \"忠诚客户\"\n",
    "    if m == 1:\n",
    "        return \"大金主\"\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c514897d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_by_c[\"客户类型\"] =  df_by_c.apply(lambda x: decide_type(x.F, x.M ,x.R), axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "b4403b66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Frequency</th>\n",
       "      <th>Monetary</th>\n",
       "      <th>Recency</th>\n",
       "      <th>F</th>\n",
       "      <th>M</th>\n",
       "      <th>R</th>\n",
       "      <th>客户类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12346.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.00</td>\n",
       "      <td>325</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12347.0</td>\n",
       "      <td>182</td>\n",
       "      <td>4310.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>最佳客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12348.0</td>\n",
       "      <td>31</td>\n",
       "      <td>1797.24</td>\n",
       "      <td>75</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>大金主</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12349.0</td>\n",
       "      <td>73</td>\n",
       "      <td>1757.55</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>大金主</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12350.0</td>\n",
       "      <td>17</td>\n",
       "      <td>334.40</td>\n",
       "      <td>310</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>大金主</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CustomerID  Frequency  Monetary  Recency  F  M  R    客户类型\n",
       "0     12346.0          2      0.00      325  4  4  4  流失廉价客户\n",
       "1     12347.0        182   4310.00        2  1  1  1    最佳客户\n",
       "2     12348.0         31   1797.24       75  3  1  3     大金主\n",
       "3     12349.0         73   1757.55       18  2  1  2     大金主\n",
       "4     12350.0         17    334.40      310  4  1  4     大金主"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_c.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "03accf97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户类型\n",
       "大金主       3064\n",
       "最佳客户       533\n",
       "忠诚客户       347\n",
       "近流失客户      154\n",
       "流失客户        47\n",
       "流失廉价客户      39\n",
       "dtype: int64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_by_c_count = df_by_c.groupby(\"客户类型\").size().sort_values(ascending=False)\n",
    "df_by_c_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "70dc1fdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('大金主', 3064),\n",
       " ('最佳客户', 533),\n",
       " ('忠诚客户', 347),\n",
       " ('近流失客户', 154),\n",
       " ('流失客户', 47),\n",
       " ('流失廉价客户', 39)]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datas = list(zip(df_by_c_count.index.to_list(), df_by_c_count.to_list()))\n",
    "datas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "5226f7e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Pie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4c8cd486",
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_pie(datas, title) -> Pie:\n",
    "    \"\"\" 创建饼图对象\n",
    "    文档地址：https://pyecharts.org/#/zh-cn/basic_charts?id=pie%ef%bc%9a%e9%a5%bc%e5%9b%be\n",
    "    @param datas: 数据，形式为[('类型1', 数据1), ('类型2', 数据2), ('类型3', 数据3)]\n",
    "    @param title: 图表的标题\n",
    "    \"\"\"\n",
    "    pie = Pie()\n",
    "    pie.add(\"\", datas)\n",
    "    pie.set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=title),\n",
    "        legend_opts=opts.LegendOpts(pos_right=\"right\")\n",
    "    )\n",
    "    pie.set_series_opts(label_opts=opts.LabelOpts(formatter=\"{b}: {c}: {d}%\"))\n",
    "    return pie"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24784ab0",
   "metadata": {},
   "source": [
    "### a）可视化展示每一类用户数占总数的比例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "93f928b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"368c740650594614a6c6a4a2f99fa946\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_368c740650594614a6c6a4a2f99fa946 = echarts.init(\n",
       "                    document.getElementById('368c740650594614a6c6a4a2f99fa946'), 'white', {renderer: 'canvas'});\n",
       "                var option_368c740650594614a6c6a4a2f99fa946 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u5927\\u91d1\\u4e3b\",\n",
       "                    \"value\": 3064\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6700\\u4f73\\u5ba2\\u6237\",\n",
       "                    \"value\": 533\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5fe0\\u8bda\\u5ba2\\u6237\",\n",
       "                    \"value\": 347\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fd1\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                    \"value\": 154\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                    \"value\": 47\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6d41\\u5931\\u5ec9\\u4ef7\\u5ba2\\u6237\",\n",
       "                    \"value\": 39\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"50%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {c}: {d}%\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u5927\\u91d1\\u4e3b\",\n",
       "                \"\\u6700\\u4f73\\u5ba2\\u6237\",\n",
       "                \"\\u5fe0\\u8bda\\u5ba2\\u6237\",\n",
       "                \"\\u8fd1\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                \"\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                \"\\u6d41\\u5931\\u5ec9\\u4ef7\\u5ba2\\u6237\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"right\": \"right\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u997c\\u56fe-\\u5ba2\\u6237\\u7c7b\\u578b\\u5bf9\\u6bd4\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_368c740650594614a6c6a4a2f99fa946.setOption(option_368c740650594614a6c6a4a2f99fa946);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x25dcedda580>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pie = create_pie(datas, \"饼图-客户类型对比\")\n",
    "pie.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1ccb88e",
   "metadata": {},
   "source": [
    "#### 取出\"CustomerID\",  \"客户类型\"这两列，准备与df合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "654f7a13",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = df_by_c[[\"CustomerID\",  \"客户类型\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "0bde0cdf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th>StockCode</th>\n",
       "      <th>Description</th>\n",
       "      <th>Quantity</th>\n",
       "      <th>InvoiceDate</th>\n",
       "      <th>UnitPrice</th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Country</th>\n",
       "      <th>target_time</th>\n",
       "      <th>Recency</th>\n",
       "      <th>total</th>\n",
       "      <th>星期</th>\n",
       "      <th>月份</th>\n",
       "      <th>客户类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>536365</td>\n",
       "      <td>85123A</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.55</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>15.30</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>536365</td>\n",
       "      <td>71053</td>\n",
       "      <td>WHITE METAL LANTERN</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>536365</td>\n",
       "      <td>84406B</td>\n",
       "      <td>CREAM CUPID HEARTS COAT HANGER</td>\n",
       "      <td>8</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.75</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>22.00</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029G</td>\n",
       "      <td>KNITTED UNION FLAG HOT WATER BOTTLE</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029E</td>\n",
       "      <td>RED WOOLLY HOTTIE WHITE HEART.</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 15:34:00</td>\n",
       "      <td>20.34</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失客户</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  InvoiceNo StockCode                          Description  Quantity  \\\n",
       "0    536365    85123A   WHITE HANGING HEART T-LIGHT HOLDER         6   \n",
       "1    536365     71053                  WHITE METAL LANTERN         6   \n",
       "2    536365    84406B       CREAM CUPID HEARTS COAT HANGER         8   \n",
       "3    536365    84029G  KNITTED UNION FLAG HOT WATER BOTTLE         6   \n",
       "4    536365    84029E       RED WOOLLY HOTTIE WHITE HEART.         6   \n",
       "\n",
       "          InvoiceDate  UnitPrice  CustomerID         Country target_time  \\\n",
       "0 2010-12-01 08:26:00       2.55     17850.0  United Kingdom  2011-12-10   \n",
       "1 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "2 2010-12-01 08:26:00       2.75     17850.0  United Kingdom  2011-12-10   \n",
       "3 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "4 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  2011-12-10   \n",
       "\n",
       "            Recency  total  星期  月份  客户类型  \n",
       "0 373 days 15:34:00  15.30   3  12  流失客户  \n",
       "1 373 days 15:34:00  20.34   3  12  流失客户  \n",
       "2 373 days 15:34:00  22.00   3  12  流失客户  \n",
       "3 373 days 15:34:00  20.34   3  12  流失客户  \n",
       "4 373 days 15:34:00  20.34   3  12  流失客户  "
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new = pd.merge(df, df1, on=\"CustomerID\")\n",
    "new.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "d0801e6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户类型\n",
       "最佳客户      4106703.970\n",
       "大金主       2472418.943\n",
       "忠诚客户      1270195.150\n",
       "近流失客户      364926.870\n",
       "流失客户        86803.711\n",
       "流失廉价客户     -11011.550\n",
       "Name: total, dtype: float64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "by_c_t = new.groupby(\"客户类型\")[\"total\"].sum().sort_values(ascending=False)\n",
    "by_c_t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "5d901c33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('最佳客户', 4106703.970000173),\n",
       " ('大金主', 2472418.9429999804),\n",
       " ('忠诚客户', 1270195.1499999189),\n",
       " ('近流失客户', 364926.870000011),\n",
       " ('流失客户', 86803.71099999893),\n",
       " ('流失廉价客户', -11011.549999999996)]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datas = list(zip(by_c_t.index.to_list(), by_c_t.to_list()))\n",
    "datas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "495fac90",
   "metadata": {},
   "source": [
    "### b) 可视化展示每一类用户消费额占总消费额的比例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "c97b3ed8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"e1294263014548e5bc0524bdb376e8a2\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_e1294263014548e5bc0524bdb376e8a2 = echarts.init(\n",
       "                    document.getElementById('e1294263014548e5bc0524bdb376e8a2'), 'white', {renderer: 'canvas'});\n",
       "                var option_e1294263014548e5bc0524bdb376e8a2 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"pie\",\n",
       "            \"clockwise\": true,\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u6700\\u4f73\\u5ba2\\u6237\",\n",
       "                    \"value\": 4106703.970000173\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5927\\u91d1\\u4e3b\",\n",
       "                    \"value\": 2472418.9429999804\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u5fe0\\u8bda\\u5ba2\\u6237\",\n",
       "                    \"value\": 1270195.1499999189\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u8fd1\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                    \"value\": 364926.870000011\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                    \"value\": 86803.71099999893\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"\\u6d41\\u5931\\u5ec9\\u4ef7\\u5ba2\\u6237\",\n",
       "                    \"value\": -11011.549999999996\n",
       "                }\n",
       "            ],\n",
       "            \"radius\": [\n",
       "                \"0%\",\n",
       "                \"75%\"\n",
       "            ],\n",
       "            \"center\": [\n",
       "                \"50%\",\n",
       "                \"50%\"\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"formatter\": \"{b}: {c}: {d}%\"\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u6700\\u4f73\\u5ba2\\u6237\",\n",
       "                \"\\u5927\\u91d1\\u4e3b\",\n",
       "                \"\\u5fe0\\u8bda\\u5ba2\\u6237\",\n",
       "                \"\\u8fd1\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                \"\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                \"\\u6d41\\u5931\\u5ec9\\u4ef7\\u5ba2\\u6237\"\n",
       "            ],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"right\": \"right\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u997c\\u56fe-\\u5ba2\\u6237\\u9500\\u552e\\u989d\\u5bf9\\u6bd4\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_e1294263014548e5bc0524bdb376e8a2.setOption(option_e1294263014548e5bc0524bdb376e8a2);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x25dceddabe0>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pie = create_pie(datas, \"饼图-客户销售额对比\")\n",
    "pie.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1eca991b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "25bc46fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户类型</th>\n",
       "      <th>月份</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>大金主</td>\n",
       "      <td>1</td>\n",
       "      <td>135462.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>大金主</td>\n",
       "      <td>2</td>\n",
       "      <td>129977.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>大金主</td>\n",
       "      <td>3</td>\n",
       "      <td>168865.71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>大金主</td>\n",
       "      <td>4</td>\n",
       "      <td>151647.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>大金主</td>\n",
       "      <td>5</td>\n",
       "      <td>191282.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>近流失客户</td>\n",
       "      <td>7</td>\n",
       "      <td>29495.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>近流失客户</td>\n",
       "      <td>8</td>\n",
       "      <td>49171.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>近流失客户</td>\n",
       "      <td>9</td>\n",
       "      <td>47463.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>近流失客户</td>\n",
       "      <td>10</td>\n",
       "      <td>54139.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>近流失客户</td>\n",
       "      <td>12</td>\n",
       "      <td>18520.41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>63 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户类型  月份      total\n",
       "0     大金主   1  135462.27\n",
       "1     大金主   2  129977.20\n",
       "2     大金主   3  168865.71\n",
       "3     大金主   4  151647.03\n",
       "4     大金主   5  191282.10\n",
       "..    ...  ..        ...\n",
       "58  近流失客户   7   29495.62\n",
       "59  近流失客户   8   49171.92\n",
       "60  近流失客户   9   47463.17\n",
       "61  近流失客户  10   54139.77\n",
       "62  近流失客户  12   18520.41\n",
       "\n",
       "[63 rows x 3 columns]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "by_c_m = new.groupby([\"客户类型\",\"月份\"])[\"total\"].sum().reset_index()\n",
    "by_c_m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "a6913d4a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"89f6a9ab88ac492e960a52df18934bc9\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_89f6a9ab88ac492e960a52df18934bc9 = echarts.init(\n",
       "                    document.getElementById('89f6a9ab88ac492e960a52df18934bc9'), 'white', {renderer: 'canvas'});\n",
       "                var option_89f6a9ab88ac492e960a52df18934bc9 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"name\": \"\\u6700\\u4f73\\u5ba2\\u6237\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 4,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    1,\n",
       "                    233871.87000000212\n",
       "                ],\n",
       "                [\n",
       "                    2,\n",
       "                    201384.1900000017\n",
       "                ],\n",
       "                [\n",
       "                    3,\n",
       "                    263437.62000000256\n",
       "                ],\n",
       "                [\n",
       "                    4,\n",
       "                    173300.90000000023\n",
       "                ],\n",
       "                [\n",
       "                    5,\n",
       "                    312812.2400000014\n",
       "                ],\n",
       "                [\n",
       "                    6,\n",
       "                    289177.9800000027\n",
       "                ],\n",
       "                [\n",
       "                    7,\n",
       "                    276726.93999999936\n",
       "                ],\n",
       "                [\n",
       "                    8,\n",
       "                    271406.6999999977\n",
       "                ],\n",
       "                [\n",
       "                    9,\n",
       "                    478326.19000001287\n",
       "                ],\n",
       "                [\n",
       "                    10,\n",
       "                    472607.18000002025\n",
       "                ],\n",
       "                [\n",
       "                    11,\n",
       "                    598394.480000009\n",
       "                ],\n",
       "                [\n",
       "                    12,\n",
       "                    535257.6800000126\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"name\": \"\\u5fe0\\u8bda\\u5ba2\\u6237\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 4,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    1,\n",
       "                    71351.29999999935\n",
       "                ],\n",
       "                [\n",
       "                    2,\n",
       "                    75767.03999999964\n",
       "                ],\n",
       "                [\n",
       "                    3,\n",
       "                    111142.99999999895\n",
       "                ],\n",
       "                [\n",
       "                    4,\n",
       "                    62027.33999999993\n",
       "                ],\n",
       "                [\n",
       "                    5,\n",
       "                    99420.19999999934\n",
       "                ],\n",
       "                [\n",
       "                    6,\n",
       "                    118792.15999999891\n",
       "                ],\n",
       "                [\n",
       "                    7,\n",
       "                    88426.20999999985\n",
       "                ],\n",
       "                [\n",
       "                    8,\n",
       "                    122533.24999999994\n",
       "                ],\n",
       "                [\n",
       "                    9,\n",
       "                    107700.40999999955\n",
       "                ],\n",
       "                [\n",
       "                    10,\n",
       "                    155834.29999999903\n",
       "                ],\n",
       "                [\n",
       "                    11,\n",
       "                    179444.9099999998\n",
       "                ],\n",
       "                [\n",
       "                    12,\n",
       "                    77755.02999999898\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"name\": \"\\u5927\\u91d1\\u4e3b\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 4,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    1,\n",
       "                    135462.2699999992\n",
       "                ],\n",
       "                [\n",
       "                    2,\n",
       "                    129977.19999999824\n",
       "                ],\n",
       "                [\n",
       "                    3,\n",
       "                    168865.70999999874\n",
       "                ],\n",
       "                [\n",
       "                    4,\n",
       "                    151647.0299999995\n",
       "                ],\n",
       "                [\n",
       "                    5,\n",
       "                    191282.10000000012\n",
       "                ],\n",
       "                [\n",
       "                    6,\n",
       "                    157604.5899999992\n",
       "                ],\n",
       "                [\n",
       "                    7,\n",
       "                    180485.72099999952\n",
       "                ],\n",
       "                [\n",
       "                    8,\n",
       "                    172925.14999999976\n",
       "                ],\n",
       "                [\n",
       "                    9,\n",
       "                    295499.30199999845\n",
       "                ],\n",
       "                [\n",
       "                    10,\n",
       "                    291682.7300000026\n",
       "                ],\n",
       "                [\n",
       "                    11,\n",
       "                    352951.56000001315\n",
       "                ],\n",
       "                [\n",
       "                    12,\n",
       "                    244035.58000000112\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"name\": \"\\u8fd1\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 4,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    1,\n",
       "                    21165.97000000005\n",
       "                ],\n",
       "                [\n",
       "                    2,\n",
       "                    21226.760000000013\n",
       "                ],\n",
       "                [\n",
       "                    3,\n",
       "                    28589.120000000083\n",
       "                ],\n",
       "                [\n",
       "                    4,\n",
       "                    28471.24000000005\n",
       "                ],\n",
       "                [\n",
       "                    5,\n",
       "                    32673.27000000007\n",
       "                ],\n",
       "                [\n",
       "                    6,\n",
       "                    34009.62000000002\n",
       "                ],\n",
       "                [\n",
       "                    7,\n",
       "                    29495.62000000008\n",
       "                ],\n",
       "                [\n",
       "                    8,\n",
       "                    49171.92000000011\n",
       "                ],\n",
       "                [\n",
       "                    9,\n",
       "                    47463.16999999984\n",
       "                ],\n",
       "                [\n",
       "                    10,\n",
       "                    54139.76999999969\n",
       "                ],\n",
       "                [\n",
       "                    11,\n",
       "                    18520.410000000044\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"line\",\n",
       "            \"name\": \"\\u6d41\\u5931\\u5ec9\\u4ef7\\u5ba2\\u6237\",\n",
       "            \"connectNulls\": false,\n",
       "            \"symbolSize\": 4,\n",
       "            \"showSymbol\": true,\n",
       "            \"smooth\": false,\n",
       "            \"clip\": true,\n",
       "            \"step\": false,\n",
       "            \"data\": [\n",
       "                [\n",
       "                    1,\n",
       "                    -2013.7599999999948\n",
       "                ],\n",
       "                [\n",
       "                    2,\n",
       "                    -222.0\n",
       "                ],\n",
       "                [\n",
       "                    3,\n",
       "                    -14.95\n",
       "                ],\n",
       "                [\n",
       "                    4,\n",
       "                    116.05\n",
       "                ],\n",
       "                [\n",
       "                    5,\n",
       "                    -103.29999999999998\n",
       "                ],\n",
       "                [\n",
       "                    6,\n",
       "                    7.899999999999999\n",
       "                ],\n",
       "                [\n",
       "                    7,\n",
       "                    -6710.73\n",
       "                ],\n",
       "                [\n",
       "                    8,\n",
       "                    -2070.7600000000007\n",
       "                ]\n",
       "            ],\n",
       "            \"hoverAnimation\": true,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 0\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u6700\\u4f73\\u5ba2\\u6237\",\n",
       "                \"\\u5fe0\\u8bda\\u5ba2\\u6237\",\n",
       "                \"\\u5927\\u91d1\\u4e3b\",\n",
       "                \"\\u8fd1\\u6d41\\u5931\\u5ba2\\u6237\",\n",
       "                \"\\u6d41\\u5931\\u5ec9\\u4ef7\\u5ba2\\u6237\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u6700\\u4f73\\u5ba2\\u6237\": true,\n",
       "                \"\\u5fe0\\u8bda\\u5ba2\\u6237\": true,\n",
       "                \"\\u5927\\u91d1\\u4e3b\": true,\n",
       "                \"\\u8fd1\\u6d41\\u5931\\u5ba2\\u6237\": true,\n",
       "                \"\\u6d41\\u5931\\u5ec9\\u4ef7\\u5ba2\\u6237\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                1,\n",
       "                2,\n",
       "                3,\n",
       "                4,\n",
       "                5,\n",
       "                6,\n",
       "                7,\n",
       "                8,\n",
       "                9,\n",
       "                10,\n",
       "                11,\n",
       "                12\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5404\\u7c7b\\u578b\\u5ba2\\u6237\\u9500\\u552e\\u989d\\u6298\\u7ebf\\u56fe\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_89f6a9ab88ac492e960a52df18934bc9.setOption(option_89f6a9ab88ac492e960a52df18934bc9);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x25dcede5910>"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = (\n",
    "    Line()\n",
    "    .add_xaxis([i for i in range(1,13)])\n",
    "    .add_yaxis(\"最佳客户\", by_c_m[by_c_m[\"客户类型\"]==\"最佳客户\"][\"total\"].to_list())\n",
    "    .add_yaxis(\"忠诚客户\", by_c_m[by_c_m[\"客户类型\"]==\"忠诚客户\"][\"total\"].to_list())\n",
    "    .add_yaxis(\"大金主\", by_c_m[by_c_m[\"客户类型\"]==\"大金主\"][\"total\"].to_list())\n",
    "    .add_yaxis(\"近流失客户\", by_c_m[by_c_m[\"客户类型\"]==\"近流失客户\"][\"total\"].to_list())\n",
    "    .add_yaxis(\"流失廉价客户\", by_c_m[by_c_m[\"客户类型\"]==\"流失廉价客户\"][\"total\"].to_list())\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"各类型客户销售额折线图\"))\n",
    ")\n",
    "c.render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed81bcd8",
   "metadata": {},
   "source": [
    "# 7.基于这些数据，还能获取哪些信息？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b1ce100",
   "metadata": {},
   "source": [
    "#### 基于以上信息，我发现了数据集本身可能有一些问题。为什么销售额会出现负数呢？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7401026",
   "metadata": {},
   "source": [
    "#### 于是看了一下廉价流失客户的一些数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "93e2c8cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th>StockCode</th>\n",
       "      <th>Description</th>\n",
       "      <th>Quantity</th>\n",
       "      <th>InvoiceDate</th>\n",
       "      <th>UnitPrice</th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Country</th>\n",
       "      <th>target_time</th>\n",
       "      <th>Recency</th>\n",
       "      <th>total</th>\n",
       "      <th>星期</th>\n",
       "      <th>月份</th>\n",
       "      <th>客户类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6551</th>\n",
       "      <td>C536391</td>\n",
       "      <td>22556</td>\n",
       "      <td>PLASTERS IN TIN CIRCUS PARADE</td>\n",
       "      <td>-12</td>\n",
       "      <td>2010-12-01 10:24:00</td>\n",
       "      <td>1.65</td>\n",
       "      <td>17548.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 13:36:00</td>\n",
       "      <td>-19.80</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6552</th>\n",
       "      <td>C536391</td>\n",
       "      <td>21984</td>\n",
       "      <td>PACK OF 12 PINK PAISLEY TISSUES</td>\n",
       "      <td>-24</td>\n",
       "      <td>2010-12-01 10:24:00</td>\n",
       "      <td>0.29</td>\n",
       "      <td>17548.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 13:36:00</td>\n",
       "      <td>-6.96</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6553</th>\n",
       "      <td>C536391</td>\n",
       "      <td>21983</td>\n",
       "      <td>PACK OF 12 BLUE PAISLEY TISSUES</td>\n",
       "      <td>-24</td>\n",
       "      <td>2010-12-01 10:24:00</td>\n",
       "      <td>0.29</td>\n",
       "      <td>17548.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 13:36:00</td>\n",
       "      <td>-6.96</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6554</th>\n",
       "      <td>C536391</td>\n",
       "      <td>21980</td>\n",
       "      <td>PACK OF 12 RED RETROSPOT TISSUES</td>\n",
       "      <td>-24</td>\n",
       "      <td>2010-12-01 10:24:00</td>\n",
       "      <td>0.29</td>\n",
       "      <td>17548.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 13:36:00</td>\n",
       "      <td>-6.96</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6555</th>\n",
       "      <td>C536391</td>\n",
       "      <td>21484</td>\n",
       "      <td>CHICK GREY HOT WATER BOTTLE</td>\n",
       "      <td>-12</td>\n",
       "      <td>2010-12-01 10:24:00</td>\n",
       "      <td>3.45</td>\n",
       "      <td>17548.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>373 days 13:36:00</td>\n",
       "      <td>-41.40</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>325716</th>\n",
       "      <td>C558329</td>\n",
       "      <td>21927</td>\n",
       "      <td>BLUE/CREAM STRIPE CUSHION COVER</td>\n",
       "      <td>-12</td>\n",
       "      <td>2011-06-28 12:09:00</td>\n",
       "      <td>1.25</td>\n",
       "      <td>17900.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>164 days 11:51:00</td>\n",
       "      <td>-15.00</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343872</th>\n",
       "      <td>C560372</td>\n",
       "      <td>M</td>\n",
       "      <td>Manual</td>\n",
       "      <td>-1</td>\n",
       "      <td>2011-07-18 12:26:00</td>\n",
       "      <td>4287.63</td>\n",
       "      <td>17448.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>144 days 11:34:00</td>\n",
       "      <td>-4287.63</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343962</th>\n",
       "      <td>C560420</td>\n",
       "      <td>M</td>\n",
       "      <td>Manual</td>\n",
       "      <td>-1</td>\n",
       "      <td>2011-07-18 15:11:00</td>\n",
       "      <td>1592.49</td>\n",
       "      <td>15369.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>144 days 08:49:00</td>\n",
       "      <td>-1592.49</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343963</th>\n",
       "      <td>C560430</td>\n",
       "      <td>M</td>\n",
       "      <td>Manual</td>\n",
       "      <td>-1</td>\n",
       "      <td>2011-07-18 15:21:00</td>\n",
       "      <td>611.86</td>\n",
       "      <td>13154.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>144 days 08:39:00</td>\n",
       "      <td>-611.86</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>344307</th>\n",
       "      <td>C560572</td>\n",
       "      <td>M</td>\n",
       "      <td>Manual</td>\n",
       "      <td>-1</td>\n",
       "      <td>2011-07-19 14:45:00</td>\n",
       "      <td>112.35</td>\n",
       "      <td>17065.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "      <td>2011-12-10</td>\n",
       "      <td>143 days 09:15:00</td>\n",
       "      <td>-112.35</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>流失廉价客户</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>145 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       InvoiceNo StockCode                        Description  Quantity  \\\n",
       "6551     C536391     22556     PLASTERS IN TIN CIRCUS PARADE        -12   \n",
       "6552     C536391     21984   PACK OF 12 PINK PAISLEY TISSUES        -24   \n",
       "6553     C536391     21983   PACK OF 12 BLUE PAISLEY TISSUES        -24   \n",
       "6554     C536391     21980  PACK OF 12 RED RETROSPOT TISSUES        -24   \n",
       "6555     C536391     21484        CHICK GREY HOT WATER BOTTLE       -12   \n",
       "...          ...       ...                                ...       ...   \n",
       "325716   C558329     21927   BLUE/CREAM STRIPE CUSHION COVER        -12   \n",
       "343872   C560372         M                             Manual        -1   \n",
       "343962   C560420         M                             Manual        -1   \n",
       "343963   C560430         M                             Manual        -1   \n",
       "344307   C560572         M                             Manual        -1   \n",
       "\n",
       "               InvoiceDate  UnitPrice  CustomerID         Country target_time  \\\n",
       "6551   2010-12-01 10:24:00       1.65     17548.0  United Kingdom  2011-12-10   \n",
       "6552   2010-12-01 10:24:00       0.29     17548.0  United Kingdom  2011-12-10   \n",
       "6553   2010-12-01 10:24:00       0.29     17548.0  United Kingdom  2011-12-10   \n",
       "6554   2010-12-01 10:24:00       0.29     17548.0  United Kingdom  2011-12-10   \n",
       "6555   2010-12-01 10:24:00       3.45     17548.0  United Kingdom  2011-12-10   \n",
       "...                    ...        ...         ...             ...         ...   \n",
       "325716 2011-06-28 12:09:00       1.25     17900.0  United Kingdom  2011-12-10   \n",
       "343872 2011-07-18 12:26:00    4287.63     17448.0  United Kingdom  2011-12-10   \n",
       "343962 2011-07-18 15:11:00    1592.49     15369.0  United Kingdom  2011-12-10   \n",
       "343963 2011-07-18 15:21:00     611.86     13154.0  United Kingdom  2011-12-10   \n",
       "344307 2011-07-19 14:45:00     112.35     17065.0  United Kingdom  2011-12-10   \n",
       "\n",
       "                 Recency    total  星期  月份    客户类型  \n",
       "6551   373 days 13:36:00   -19.80   3  12  流失廉价客户  \n",
       "6552   373 days 13:36:00    -6.96   3  12  流失廉价客户  \n",
       "6553   373 days 13:36:00    -6.96   3  12  流失廉价客户  \n",
       "6554   373 days 13:36:00    -6.96   3  12  流失廉价客户  \n",
       "6555   373 days 13:36:00   -41.40   3  12  流失廉价客户  \n",
       "...                  ...      ...  ..  ..     ...  \n",
       "325716 164 days 11:51:00   -15.00   2   6  流失廉价客户  \n",
       "343872 144 days 11:34:00 -4287.63   1   7  流失廉价客户  \n",
       "343962 144 days 08:49:00 -1592.49   1   7  流失廉价客户  \n",
       "343963 144 days 08:39:00  -611.86   1   7  流失廉价客户  \n",
       "344307 143 days 09:15:00  -112.35   2   7  流失廉价客户  \n",
       "\n",
       "[145 rows x 14 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new[new[\"客户类型\"]==\"流失廉价客户\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94652f07",
   "metadata": {},
   "source": [
    "### 发现Quantity一列居然有负数\n",
    "### 或许是超市倒欠客户东西了吧？嘤嘤嘤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "66a6d201",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>月份</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>475074.380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>436546.150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>579964.610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>426047.851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>648251.080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>608013.160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>574238.481</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>616368.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>931440.372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>974603.590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11</td>\n",
       "      <td>1132407.740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12</td>\n",
       "      <td>897110.400</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    月份        total\n",
       "0    1   475074.380\n",
       "1    2   436546.150\n",
       "2    3   579964.610\n",
       "3    4   426047.851\n",
       "4    5   648251.080\n",
       "5    6   608013.160\n",
       "6    7   574238.481\n",
       "7    8   616368.000\n",
       "8    9   931440.372\n",
       "9   10   974603.590\n",
       "10  11  1132407.740\n",
       "11  12   897110.400"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t_by_M = new.groupby(\"月份\")[\"total\"].sum().reset_index()\n",
    "t_by_M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "66ab2991",
   "metadata": {},
   "outputs": [],
   "source": [
    "bar = (\n",
    "    Bar()\n",
    "    .add_xaxis(t_by_M[\"月份\"].to_list())\n",
    "    .add_yaxis(\"total\", t_by_M[\"total\"].to_list())\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"各月销售总额对比图\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "1821991f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"f3738f53874641279defd8b6f1fd4726\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_f3738f53874641279defd8b6f1fd4726 = echarts.init(\n",
       "                    document.getElementById('f3738f53874641279defd8b6f1fd4726'), 'white', {renderer: 'canvas'});\n",
       "                var option_f3738f53874641279defd8b6f1fd4726 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"bar\",\n",
       "            \"name\": \"total\",\n",
       "            \"legendHoverLink\": true,\n",
       "            \"data\": [\n",
       "                475074.3800000181,\n",
       "                436546.1500000177,\n",
       "                579964.6100000126,\n",
       "                426047.851000015,\n",
       "                648251.0799999961,\n",
       "                608013.1600000068,\n",
       "                574238.4810000029,\n",
       "                616368.0000000029,\n",
       "                931440.3719999902,\n",
       "                974603.589999969,\n",
       "                1132407.73999993,\n",
       "                897110.3999999895\n",
       "            ],\n",
       "            \"showBackground\": false,\n",
       "            \"barMinHeight\": 0,\n",
       "            \"barCategoryGap\": \"20%\",\n",
       "            \"barGap\": \"30%\",\n",
       "            \"large\": false,\n",
       "            \"largeThreshold\": 400,\n",
       "            \"seriesLayoutBy\": \"column\",\n",
       "            \"datasetIndex\": 0,\n",
       "            \"clip\": true,\n",
       "            \"zlevel\": 0,\n",
       "            \"z\": 2,\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"total\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"total\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"showContent\": true,\n",
       "        \"alwaysShowContent\": false,\n",
       "        \"showDelay\": 0,\n",
       "        \"hideDelay\": 100,\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0,\n",
       "        \"padding\": 5\n",
       "    },\n",
       "    \"xAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            },\n",
       "            \"data\": [\n",
       "                1,\n",
       "                2,\n",
       "                3,\n",
       "                4,\n",
       "                5,\n",
       "                6,\n",
       "                7,\n",
       "                8,\n",
       "                9,\n",
       "                10,\n",
       "                11,\n",
       "                12\n",
       "            ]\n",
       "        }\n",
       "    ],\n",
       "    \"yAxis\": [\n",
       "        {\n",
       "            \"show\": true,\n",
       "            \"scale\": false,\n",
       "            \"nameLocation\": \"end\",\n",
       "            \"nameGap\": 15,\n",
       "            \"gridIndex\": 0,\n",
       "            \"inverse\": false,\n",
       "            \"offset\": 0,\n",
       "            \"splitNumber\": 5,\n",
       "            \"minInterval\": 0,\n",
       "            \"splitLine\": {\n",
       "                \"show\": false,\n",
       "                \"lineStyle\": {\n",
       "                    \"show\": true,\n",
       "                    \"width\": 1,\n",
       "                    \"opacity\": 1,\n",
       "                    \"curveness\": 0,\n",
       "                    \"type\": \"solid\"\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5404\\u6708\\u9500\\u552e\\u603b\\u989d\\u5bf9\\u6bd4\\u56fe\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_f3738f53874641279defd8b6f1fd4726.setOption(option_f3738f53874641279defd8b6f1fd4726);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x25dd2920430>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bar.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aed53ac4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
